/**
 * @Author: liutao
 * @Date:   2017-03-14T18:17:44+08:00
 * @Email:  947809647@qq.com
 * @Last modified by:   liutao
 * @Last modified time: 2017-04-25T15:42:38+08:00
 */
import queryString from 'query-string'

import Vue from 'vue'
const SUCCESS = 1000000
const API_ROOT = server.root + '/newmanage'

const activityListResource = Vue.resource(API_ROOT + '/task/activity/list')
const activityUpdateResource = Vue.resource(API_ROOT + '/task/activity/update')
const activityAddResource = Vue.resource(API_ROOT + '/task/activity/insert')
const customMiniListResource = Vue.resource(API_ROOT + '/activity/list')
const customAddResource = Vue.resource(API_ROOT + '/activity/add')
const customUpdateResource = Vue.resource(API_ROOT + '/activity/update')
const customPrizeListResource = Vue.resource(API_ROOT + '/prize/list')
const customPrizeAddResource = Vue.resource(API_ROOT + '/prize/add')
const customPrizeUpdateResource = Vue.resource(API_ROOT + '/prize/update')
const customPrizeResultLResource = Vue.resource(API_ROOT + '/prize/result/list')
const customPrizeCodeLResource = Vue.resource(API_ROOT + '/prize/code/list')
const customCodeRemoveResource = Vue.resource(API_ROOT + '/prize/code/remove')
const customCodeAddResource = Vue.resource(API_ROOT + '/prize/code/batchadd')
const customCodeExcelResource = Vue.resource(API_ROOT + '/prize/code/excel')
export const exportCodeResource = API_ROOT + '/prize/code/export'
const customMsgListResource = Vue.resource(API_ROOT + '/activity/msg/list')
const customMsgConfigResource = Vue.resource(API_ROOT + '/activity/msg/config')
const customMsgSendResource = Vue.resource(API_ROOT + '/activity/msg/send')
const guoxinListResource = Vue.resource(API_ROOT + '/gxopen/list')
const guoxinUpdateResource = Vue.resource(API_ROOT + '/gxopen/changestatus')
const guoxinInviteListResource = Vue.resource(API_ROOT + '/gxopen/sharelist')
const guoxinUpdateInviteResource = Vue.resource(API_ROOT + '/gxopen/sharelist/changestatus')
const customFakeRecordsResource = Vue.resource(API_ROOT + '/activity/fakeaward/list')
const customFakeAddResource = Vue.resource(API_ROOT + '/activity/fakeaward/add')
const customFakeRemoveResource = Vue.resource(API_ROOT + '/activity/fakeaward/remove')
const hualinListResource = Vue.resource(API_ROOT + '/hlopen/list')
const hualinUpdateResource = Vue.resource(API_ROOT + '/hlopen/changestatus')
const hualinInviteListResource = Vue.resource(API_ROOT + '/hlopen/sharelist')
const hualinUpdateInviteResource = Vue.resource(API_ROOT + '/hlopen/sharelist/changestatus')

// 充流量
const getDataflowListResource = Vue.resource(API_ROOT + '/dataflow/list')
const getInvalidOrderResource = Vue.resource(API_ROOT + '/dataflow/invalidOrder')
const chargeDataflowResource = Vue.resource(API_ROOT + '/dataflow/chargeDataflow')
const updateOrderResultResource = Vue.resource(API_ROOT + '/dataflow/updateOrderResult')
const getAccountResource = Vue.resource(API_ROOT + '/dataflow/account')

// 年度票选
const getVoteListResource = Vue.resource(API_ROOT + '/year/top/list')
const getAddVoteResource = Vue.resource(API_ROOT + '/year/top/votes')
const getSearchListResource = Vue.resource(API_ROOT + '/year/top/search')

// 金贝塔小游戏
const awardResource = Vue.resource(API_ROOT + '/game/no/award')
const searchResource = Vue.resource(API_ROOT + '/game/list/phone')
const gameRankResource = Vue.resource(API_ROOT + '/game/list/rank')
const gameUpdateResource = Vue.resource(API_ROOT + '/game/inc')

export const getActivityList = ({dispatch}, params) => {
  return activityListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('UPDATE_ACTIVITY_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const updateActivity = ({dispatch}, params) => {
  return activityUpdateResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_ACTIVITY_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const addActivity = ({dispatch}, params) => {
  return activityAddResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_ACTIVITY_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const customList = ({dispatch}, params) => {
  return customMiniListResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('UPDATE_CUSTOM_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const addCustom = ({dispatch}, params) => {
  return customAddResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_CUSTOM_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const updateCustom = ({dispatch}, params) => {
  return customUpdateResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_CUSTOM_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const prizeList = ({dispatch}, params) => {
  return customPrizeListResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('UPDATE_PRIZE_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const addPrize = ({dispatch}, params) => {
  return customPrizeAddResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_PRIZE_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const updatePrize = ({dispatch}, params) => {
  return customPrizeUpdateResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_PRIZE_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const prizeResultList = ({dispatch}, params) => {
  return customPrizeResultLResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const prizeCodeList = ({dispatch}, params) => {
  return customPrizeCodeLResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('IMPORT_CODE_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const codeRemoveBatch = ({dispatch}, params) => {
  return customCodeRemoveResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('IMPORT_CODE_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const codeAddBatch = ({dispatch}, params) => {
  return customCodeAddResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('IMPORT_CODE_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const codeExcel = ({dispatch}, params) => {
  return customCodeExcelResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('IMPORT_CODE_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const msgList = ({dispatch}, params) => {
  return customMsgListResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('UPDATE_MSG_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const msgConfig = ({dispatch}, params) => {
  return customMsgConfigResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_MSG_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const msgSend = ({dispatch}, params) => {
  return customMsgSendResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return null
      }
      return Promise.reject(res.data)
    })
}
export const fakeRecords = ({dispatch}, params) => {
  return customFakeRecordsResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('UPDATE_FAKE_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const fakeAdd = ({dispatch}, params) => {
  return customFakeAddResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_FAKE_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const fakeRemove = ({dispatch}, params) => {
  return customFakeRemoveResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_FAKE_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const setActivityFlag = ({dispatch}, params) => {
  dispatch('SET_ACTIVITY_ID', params)
}
export const setPrizeFlag = ({dispatch}, params) => {
  dispatch('SET_PRIZE_ID', params)
}
export const getGuoxinList = ({dispatch}, params) => {
  return guoxinListResource.get(params)
    .then((res) => {
      dispatch('UPDATE_GUOXIN_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const updateGuoxin = ({dispatch}, params) => {
  return guoxinUpdateResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_GUOXIN_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const getGuoxinInviteList = ({dispatch}, params) => {
  return guoxinInviteListResource.get(params)
    .then((res) => {
      dispatch('UPDATE_GUOXIN_INVITE_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const updateGuoxinInvite = ({dispatch}, params) => {
  return guoxinUpdateInviteResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_GUOXIN_INVITE_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const getHualinList = ({dispatch}, params) => {
  return hualinListResource.get(params)
    .then((res) => {
      dispatch('UPDATE_HUALIN_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const updateHualin = ({dispatch}, params) => {
  return hualinUpdateResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_HUALIN_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const getHualinInviteList = ({dispatch}, params) => {
  return hualinInviteListResource.get(params)
    .then((res) => {
      dispatch('UPDATE_HUALIN_INVITE_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const updateHualinInvite = ({dispatch}, params) => {
  return hualinUpdateInviteResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_HUALIN_INVITE_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

const manualImportExcelResource = Vue.resource(API_ROOT + '/prize/mini/hand')
export const manualImportExcel = ({dispatch}, params) => {
  return manualImportExcelResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

const autoImportExcelResource = Vue.resource(API_ROOT + '/prize/mini/auto')
export const autoImportExcel = ({dispatch}, params) => {
  return autoImportExcelResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

/**
 *  充流量
 */
export const getDataflowList = ({dispatch}, params) => {
  return getDataflowListResource.get(params)
    .then((res) => {
      dispatch('DATAFLOW_NEED_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

export const getInvalidOrder = ({dispatch}, params) => {
  return getInvalidOrderResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('DATAFLOW_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

export const chargeDataflow = ({dispatch}, params) => {
  return chargeDataflowResource.get(params)
    .then((res) => {
      dispatch('DATAFLOW_NEED_FRESH', 1)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

export const updateOrderResult = ({dispatch}, params) => {
  return updateOrderResultResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('DATAFLOW_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

export const getAccount = ({dispatch}) => {
  return getAccountResource.get()
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

/**
 *  大转盘抽奖活动统计
 */
const getCourseResource = Vue.resource(API_ROOT + '/activity/course/list')
export const getCourseList = ({dispatch}, params) => {
  return getCourseResource.get(params)
    .then((res) => {
      dispatch('DATAFLOW_NEED_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
const getStatisticsResource = Vue.resource(API_ROOT + '/activity/course/statistics')
export const getStatisticsList = ({dispatch}, params) => {
  return getStatisticsResource.get(params)
    .then((res) => {
      dispatch('DATAFLOW_NEED_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

/**
 * 活动
 */
export const getVoteList = ({dispatch}, params) => {
  return getVoteListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('UPDATE_VOTES_TOPLIST_FRESH', 0)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}
export const getAddVote = ({dispatch}, params) => {
  return getAddVoteResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}
export const getSearchList = ({dispatch}, params) => {
  return getSearchListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

const gameListResource = Vue.resource(API_ROOT + '/game/list')
export const getGameList = ({dispatch}, params) => {
  return gameListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const getSearchGameList = ({dispatch}, params) => {
  return searchResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const noAward = ({dispatch}, params) => {
  return awardResource
    .save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

const gameAuditRecordResource = Vue.resource(API_ROOT + '/game/audit')
export const auditRecord = ({dispatch}, params) => {
  return gameAuditRecordResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

const gameReAuditRecordResource = Vue.resource(API_ROOT + '/game/reAudit')
export const reAuditRecord = ({dispatch}, params) => {
  return gameReAuditRecordResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const getRankList = ({dispatch}, params) => {
  return gameRankResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const updateScore = ({dispatch}, params) => {
  return gameUpdateResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}
/**
 * 投顾报名
 */
const personalListResource = Vue.resource(API_ROOT + '/simulator/player/list')

export const getPersonalList = ({dispatch}, params) => {
  return personalListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CGB_LIST_NEED_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
const memberListResource = Vue.resource(API_ROOT + '/simulator/member/list')
export const getMemberList = ({dispatch}, params) => {
  return memberListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CLASSES_LIST_NEED_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
const updateAdvisorResource = Vue.resource(API_ROOT + '/simulator/player/update')
export const updateAdvisor = ({dispatch}, params) => {
  return updateAdvisorResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CGB_LIST_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const updateAdvisorUserResource = Vue.resource(API_ROOT + '/simulator/member/update')
export const updateAdvisorUser = ({dispatch}, params) => {
  return updateAdvisorUserResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CLASSES_LIST_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const deleteMemberResource = Vue.resource(API_ROOT + '/simulator/member/delete')
export const deleteMember = ({dispatch}, params) => {
  return deleteMemberResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CLASSES_LIST_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const deleteAdvisorResource = Vue.resource(API_ROOT + '/simulator/player/delete')
export const deleteAdvisor = ({dispatch}, params) => {
  return deleteAdvisorResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CGB_LIST_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

export const exportAdvisorList = ({dispatch}, params) => {
  let query = queryString.stringify(params)
  window.open(API_ROOT + '/simulator/player/export?' + query)
}

const importAdvisorResource = Vue.resource(API_ROOT + '/simulator/player/import')
export const importAdvisor = ({dispatch}, params) => {
  return importAdvisorResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('CGB_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}